$(document).ready(function() {

   $("#map").css({
      height: 500,
      width:  600
   });
   // @todo prevent from centring
   var myLatLng = new google.maps.LatLng(50.022451, 36.227070); //50.022451, 36.227070
   MYMAP.init('#map', myLatLng, 11);
   // Send ajax-requiest (should work fast as the requests might be chached)
   $.ajax({
      url: "/index/get-markers",
      dataType: "xml",
      success: function(data){
         MYMAP.placeMarkers(data);
      },
      error: function(xhr, textStatus, errorThrown){
         // @todo process error if make up something
     }
   });
});

var MYMAP = {
   map: null,
   bounds: null
}

MYMAP.init = function(selector, latLng, zoom) {
   var myOptions = {
      zoom:zoom,
      center: latLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
   }
   MYMAP.map = new google.maps.Map($(selector)[0], myOptions);
   MYMAP.bounds = new google.maps.LatLngBounds();
   MYMAP.centerLocation();
}

MYMAP.centerLocation = function() {
   var initialLocation;
   var browserSupportFlag =  new Boolean();
   // Try W3C Geolocation (Preferred)
   if(navigator.geolocation) {
      browserSupportFlag = true;
      navigator.geolocation.getCurrentPosition(function(position) {
         initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
         MYMAP.map.setCenter(initialLocation);
      }, function() {
         MYMAP.handleNoGeolocation(browserSupportFlag);
      });
   // Try Google Gears Geolocation
   } else if (google.gears) {
      browserSupportFlag = true;
      var geo = google.gears.factory.create('beta.geolocation');
      geo.getCurrentPosition(function(position) {
         initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
         MYMAP.map.setCenter(initialLocation);
      }, function() {
         MYMAP.handleNoGeoLocation(browserSupportFlag);
      });
   // Browser doesn't support Geolocation
   } else {
      browserSupportFlag = false;
      MYMAP.handleNoGeolocation(browserSupportFlag);
   }
}


MYMAP.handleNoGeoLocation = function(errorFlag) {
   // default center is kiev as we all live in ukraine
   var kiev = new google.maps.LatLng(50.4501, 30.5234);
   var initialLocation;
   if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = kiev;
   } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = kiev;
   }
   MYMAP.map.setCenter(initialLocation);
}

MYMAP.placeMarkers = function(xml) {
   $(xml).find("marker").each(function(){
      var name = $(this).find('name').text();
      var address = $(this).find('address').text();
      //var markerImage = $(this).find('image').text();
      // create a new LatLng point for the marker
      var lat = $(this).find('lat').text();
      var lng = $(this).find('lng').text();
      var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

      // extend the bounds to include the new point
      MYMAP.bounds.extend(point);
      var markerImage = '/images/icon.jpg';
      
      var marker = new google.maps.Marker({
         icon: markerImage,
         position: point,
         map: MYMAP.map
      });

      var infoWindow = new google.maps.InfoWindow();
      var html='<strong>'+name+'</strong.><br />'+address;
      google.maps.event.addListener(marker, 'click', function() {
         infoWindow.setContent(html);
         infoWindow.open(MYMAP.map, marker);
      });
      MYMAP.map.fitBounds(MYMAP.bounds);
   });
}